using System;
using System.Collections.Generic;
using System.Linq;
using Managistics.Core.Infrastructure;
using Managistics.Core.Repositories;
using Managistics.Core.Services;
using Managistics.Model;

namespace Managistics.Core.Services.Impl
{
    public class PurchaseReportService : IPurchaseReportService
    {
        private readonly IPurchaseOrderReportRepository _purchaseOrderReportRepository;
        private readonly IPurchaseOrderRepository _purchaseOrderRepository;

        public PurchaseReportService(IPurchaseOrderReportRepository purchaseOrderReportRepository, IPurchaseOrderRepository purchaseOrderRepository)
        {
            this._purchaseOrderReportRepository = purchaseOrderReportRepository;
            this._purchaseOrderRepository = purchaseOrderRepository;
        }

        [UnitOfWork]
        public Tuple<IList<PurchaseOrderReport>, IList<PurchaseOrderedTotalRecord>> GetPurchaseOrderedDetail(PurchaseReportsVO parameters)
        {
            IList<PurchaseOrderReport> orderedDetail = new List<PurchaseOrderReport>();
            IList<PurchaseOrderedTotalRecord> sumOrderedDetail = new List<PurchaseOrderedTotalRecord>();
            try
            {
                orderedDetail = _purchaseOrderReportRepository.GetPurchaseOrderedDetailByGoodOrSupplier(parameters.ReportBy, parameters.StartDate, parameters.EndDate, parameters.SupplierCode, parameters.GoodCode);

                // config to enum report type: Good = 1, Supplier = 2
                if (parameters.ReportBy == 1)
                {
                    // get sum ordered quantity, price
                    sumOrderedDetail =
                        (from d in orderedDetail
                         orderby d.ItemId ascending
                         group d by d.ItemId into GroupOrderedDetail
                         //let itemId = GroupOrderedDetail.Select(g => g.ItemId)
                         select new PurchaseOrderedTotalRecord
                         {
                             ObjectId = GroupOrderedDetail.Select(g => g.ItemId).First(),
                             ObjectName = GroupOrderedDetail.Select(g => g.ItemName).First(),
                             SumOrderQuantity = GroupOrderedDetail.Sum(g => g.OrderQuantity),
                             SumOrderPriceVat = GroupOrderedDetail.Sum(g => g.TotalPriceVat)
                         }).ToList<PurchaseOrderedTotalRecord>();
                }
                else
                {
                    // get sum ordered quantity, price
                    sumOrderedDetail =
                        (from d in orderedDetail
                         orderby d.Supplierid ascending
                         group d by d.Supplierid into GroupOrderedDetail
                         select new PurchaseOrderedTotalRecord
                         {
                             ObjectId = GroupOrderedDetail.Select(g => g.Supplierid).First(),
                             ObjectName = GroupOrderedDetail.Select(g => g.SupplierName).First(),
                             SumOrderQuantity = GroupOrderedDetail.Sum(g => g.OrderQuantity),
                             SumOrderPriceVat = GroupOrderedDetail.Sum(g => g.TotalPriceVat)
                         }).ToList<PurchaseOrderedTotalRecord>();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message, ex);
            }

            return new Tuple<IList<PurchaseOrderReport>, IList<PurchaseOrderedTotalRecord>>(orderedDetail, sumOrderedDetail);
        }

        [UnitOfWork]
        public Tuple<IList<PurchaseInputReport>, IList<PurchaseInputedTotalRecord>> GetPurchaseInputedDetail(PurchaseReportsVO parameters)
        {
            IList<PurchaseInputReport> inputedDetail = new List<PurchaseInputReport>();
            IList<PurchaseInputedTotalRecord> sumInputedDetail = new List<PurchaseInputedTotalRecord>();
            try
            {
                inputedDetail = _purchaseOrderReportRepository.GetPurchaseInputedDetailByGoodOrSupplier(parameters.ReportBy, parameters.StartDate, parameters.EndDate, parameters.SupplierCode, parameters.GoodCode);
                if (parameters.ReportBy == 1)
                {
                    sumInputedDetail = this.GetSumInputedTotalByGood(inputedDetail);
                }
                else
                {
                    sumInputedDetail = this.GetSumInputedTotalBySupplier(inputedDetail);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message, ex);
            }

            return new Tuple<IList<PurchaseInputReport>, IList<PurchaseInputedTotalRecord>>(inputedDetail, sumInputedDetail);
        }

        [UnitOfWork]
        public Tuple<IList<PurchaseInputReport>, IList<PurchaseBacklogTotalRecord>> GetPurchaseBacklogDetail(PurchaseReportsVO parameters)
        {
            IList<PurchaseInputReport> inputedDetail = new List<PurchaseInputReport>();
            IList<PurchaseInputReport> existingDetail = new List<PurchaseInputReport>();
            IList<PurchaseBacklogTotalRecord> sumBacklogDetail = new List<PurchaseBacklogTotalRecord>();
            try
            {
                inputedDetail = _purchaseOrderReportRepository.GetPurchaseInputedDetailByGoodOrSupplier(parameters.ReportBy, parameters.StartDate, parameters.EndDate, parameters.SupplierCode, parameters.GoodCode);
                if (parameters.ReportBy == 1)
                {
                    // only get records min of existing quantity
                    var existing = (from i in inputedDetail
                                    group i by new { i.ItemId, i.OrderId } into InputedDetail
                                    where InputedDetail.Min(d => d.ExistingQty) != null && InputedDetail.Min(d => d.ExistingQty.Value) > 0
                                    select new
                                    {
                                        OrderId = InputedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                        ItemId = InputedDetail.Select(d => d.ItemId).FirstOrDefault(),
                                        ItemName = InputedDetail.Select(d => d.ItemName).FirstOrDefault(),
                                        ExistingQuantity = InputedDetail.Min(d => d.ExistingQty),
                                        ExistingPrice = InputedDetail.Min(d => d.TotalExistingPrice)
                                    });

                    existingDetail = (from i in inputedDetail
                                      join e in existing on new { i.OrderId, i.ItemId, i.ExistingQty.Value } equals new { e.OrderId, e.ItemId, e.ExistingQuantity.Value }
                                      select i).ToList<PurchaseInputReport>();

                    sumBacklogDetail = (from o in existing
                                        group o by o.ItemId into Backloged
                                        select new PurchaseBacklogTotalRecord
                                        {
                                            ObjectId = Backloged.Select(b => b.ItemId).FirstOrDefault(),
                                            ObjectName = Backloged.Select(b => b.ItemName).FirstOrDefault(),
                                            SumBacklogQuantity = Backloged.Sum(b => b.ExistingQuantity),
                                            SumBacklogPrice = Backloged.Sum(b => b.ExistingPrice)
                                        }).ToList<PurchaseBacklogTotalRecord>();
                }
                else
                {
                    // only get records min of existing quantity
                    var existing = (from i in inputedDetail
                                    group i by new { i.Supplierid, i.OrderId, i.ItemId } into InputedDetail
                                    where InputedDetail.Min(d => d.ExistingQty) != null && InputedDetail.Min(d => d.ExistingQty.Value) > 0
                                    select new
                                    {
                                        SupplierId = InputedDetail.Select(d => d.Supplierid).FirstOrDefault(),
                                        OrderId = InputedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                        ItemId = InputedDetail.Select(d => d.ItemId).FirstOrDefault(),
                                        SupplierName = InputedDetail.Select(d => d.SupplierName).FirstOrDefault(),
                                        ExistingQuantity = InputedDetail.Min(d => d.ExistingQty),
                                        ExistingPrice = InputedDetail.Min(d => d.TotalExistingPrice)
                                    });

                    existingDetail = (from i in inputedDetail
                                      //http://social.msdn.microsoft.com/Forums/en-US/bf98ec7a-cb80-4901-8eb2-3aa6636a4fde/linq-join-error-the-type-of-one-of-the-expressions-in-the-join-clause-is-incorrect-type-inference
                                      join e in existing on new { i.Supplierid, i.OrderId, i.ItemId } equals new { Supplierid = e.SupplierId, e.OrderId, e.ItemId }
                                      select i).ToList<PurchaseInputReport>();

                    sumBacklogDetail = (from o in existing
                                        group o by o.SupplierId into Backloged
                                        select new PurchaseBacklogTotalRecord
                                        {
                                            ObjectId = Backloged.Select(b => b.SupplierId).FirstOrDefault(),
                                            ObjectName = Backloged.Select(b => b.SupplierName).FirstOrDefault(),
                                            SumBacklogQuantity = Backloged.Sum(b => b.ExistingQuantity),
                                            SumBacklogPrice = Backloged.Sum(b => b.ExistingPrice)
                                        }).ToList<PurchaseBacklogTotalRecord>();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message, ex);
            }

            return new Tuple<IList<PurchaseInputReport>, IList<PurchaseBacklogTotalRecord>>(existingDetail, sumBacklogDetail);
        }

        [UnitOfWork]
        public Tuple<IList<PurchaseInputReport>, IList<PurchaseInputedTotalRecord>> GetPurchaseNonInputDetail(PurchaseReportsVO parameters)
        {
            IList<PurchaseInputReport> inputedDetail = new List<PurchaseInputReport>();
            IList<PurchaseInputReport> allNonInput = new List<PurchaseInputReport>();
            IList<PurchaseInputedTotalRecord> sumNonInputedDetail = new List<PurchaseInputedTotalRecord>();
            try
            {
                inputedDetail = _purchaseOrderReportRepository.GetPurchaseInputedDetailByGoodOrSupplier(parameters.ReportBy, parameters.StartDate, parameters.EndDate, parameters.SupplierCode, parameters.GoodCode);

                var neverInput = this.GetOrderItemNeverInput(inputedDetail, parameters.StartDate, parameters.EndDate, parameters.SupplierCode, parameters.GoodCode);

                if (parameters.ReportBy == 1)
                {
                    // only get records min of existing quantity - same as GetPurchaseBacklogDetail
                    var existing = (from i in inputedDetail
                                    group i by new { i.ItemId, i.OrderId } into InputedDetail
                                    where InputedDetail.Min(d => d.ExistingQty) != null && InputedDetail.Min(d => d.ExistingQty.Value) > 0
                                    select new
                                    {
                                        OrderId = InputedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                        ItemId = InputedDetail.Select(d => d.ItemId).FirstOrDefault(),
                                        ItemName = InputedDetail.Select(d => d.ItemName).FirstOrDefault(),
                                        ExistingQuantity = InputedDetail.Min(d => d.ExistingQty),
                                        ExistingPrice = InputedDetail.Min(d => d.TotalExistingPrice)
                                    });

                    // get number outputed of purchase by getting order total - existing quanty
                    var existingDetail = (from i in inputedDetail
                                          join e in existing on new { i.OrderId, i.ItemId, i.ExistingQty.Value } equals new { e.OrderId, e.ItemId, e.ExistingQuantity.Value }
                                          orderby e.ItemId ascending
                                          select i).ToList<PurchaseInputReport>();
                    existingDetail.All(x =>
                    {
                        x.InputQuantity = x.OrderQuantity.Value - x.ExistingQty.Value;
                        x.TotalInputPrice = x.TotalPriceVat.Value - x.TotalExistingPrice.Value;
                        return true;
                    });

                    allNonInput = (from i in existingDetail.Concat(neverInput).ToList()
                                   orderby i.ItemId ascending
                                   select i).ToList<PurchaseInputReport>();

                    sumNonInputedDetail = (from i in allNonInput
                                           group i by i.ItemId into Grouped
                                           select new PurchaseInputedTotalRecord
                                           {
                                               ObjectId = Grouped.Select(g => g.ItemId).FirstOrDefault(),
                                               ObjectName = Grouped.Select(g => g.ItemName).FirstOrDefault(),
                                               SumOrderQuantity = Grouped.Sum(g => g.OrderQuantity),
                                               SumOrderPrice = Grouped.Sum(g => g.TotalPriceVat),
                                               SumInputedQuantity = Grouped.Sum(g => g.InputQuantity),
                                               SumInputedPrice = Grouped.Sum(g => g.TotalInputPrice),
                                               SumExistingQuantity = Grouped.Sum(g => g.ExistingQty),
                                               SumExistingPrice = Grouped.Sum(g => g.TotalExistingPrice),
                                           }).ToList<PurchaseInputedTotalRecord>();
                }
                else
                {
                    // get by supplier
                    // only get records min of existing quantity - same as GetPurchaseBacklogDetail
                    var existing = (from i in inputedDetail
                                    group i by new { i.Supplierid, i.OrderId, i.ItemId } into InputedDetail
                                    where InputedDetail.Min(d => d.ExistingQty) != null && InputedDetail.Min(d => d.ExistingQty.Value) > 0
                                    select new
                                    {
                                        SupplierId = InputedDetail.Select(d => d.Supplierid).FirstOrDefault(),
                                        OrderId = InputedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                        ItemId = InputedDetail.Select(d => d.ItemId).FirstOrDefault(),
                                        SupplierName = InputedDetail.Select(d => d.SupplierName).FirstOrDefault(),
                                        ExistingQuantity = InputedDetail.Min(d => d.ExistingQty),
                                        ExistingPrice = InputedDetail.Min(d => d.TotalExistingPrice)
                                    });
                    // get number inputed of purchase by getting order total - existing quanty
                    var existingDetail = (from i in inputedDetail
                                          join e in existing on new { i.Supplierid, i.OrderId, i.ItemId, i.ExistingQty.Value } equals new { Supplierid = e.SupplierId, e.OrderId, e.ItemId, e.ExistingQuantity.Value }
                                          orderby i.Supplierid, i.OrderId, i.ItemId ascending
                                          select i).ToList<PurchaseInputReport>();
                    existingDetail.All(x =>
                    {
                        x.InputQuantity = x.OrderQuantity.Value - x.ExistingQty.Value;
                        x.TotalInputPrice = x.TotalPriceVat.Value - x.TotalExistingPrice.Value;
                        return true;
                    });

                    allNonInput = (from i in existingDetail.Concat(neverInput).ToList()
                                   orderby i.Supplierid ascending
                                   select i).ToList<PurchaseInputReport>();

                    sumNonInputedDetail = (from i in allNonInput
                                           group i by i.Supplierid into Grouped
                                           select new PurchaseInputedTotalRecord
                                           {
                                               ObjectId = Grouped.Select(g => g.Supplierid).FirstOrDefault(),
                                               ObjectName = Grouped.Select(g => g.SupplierName).FirstOrDefault(),
                                               SumOrderQuantity = Grouped.Sum(g => g.OrderQuantity),
                                               SumOrderPrice = Grouped.Sum(g => g.TotalPriceVat),
                                               SumInputedQuantity = Grouped.Sum(g => g.InputQuantity),
                                               SumInputedPrice = Grouped.Sum(g => g.TotalInputPrice),
                                               SumExistingQuantity = Grouped.Sum(g => g.ExistingQty),
                                               SumExistingPrice = Grouped.Sum(g => g.TotalExistingPrice),
                                           }).ToList<PurchaseInputedTotalRecord>();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error " + ex.Message, ex);
            }

            return new Tuple<IList<PurchaseInputReport>, IList<PurchaseInputedTotalRecord>>(allNonInput, sumNonInputedDetail);
        }

        /// <summary>
        /// Get all orders and items never input form date ranges
        /// </summary>
        /// <param name="inputedDetail"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        /// <param name="supplierCode"></param>
        /// <param name="goodCode"></param>
        /// <returns></returns>
        private IList<PurchaseInputReport> GetOrderItemNeverInput(IList<PurchaseInputReport> inputedDetail, DateTime startDate, DateTime endDate, string supplierCode, string goodCode)
        {
            // group by orderid, item id that inputed in ranges
            var groupOrderItemFromInputedDetail = (from i in inputedDetail
                                                   group i by new { i.ItemId, i.OrderId } into GroupedDetail
                                                   select new
                                                   {
                                                       OrderId = GroupedDetail.Select(g => g.OrderId).FirstOrDefault(),
                                                       ItemId = GroupedDetail.Select(g => g.ItemId).FirstOrDefault()
                                                   });

            // get all ordered purchases, order id, item
            var allOrderedPurchases = _purchaseOrderRepository.GetPurchaseOrderedDetail(startDate, endDate, supplierCode, goodCode);

            var neverInput =
                    (from d in
                         (from ordered in allOrderedPurchases
                          join inputed in groupOrderItemFromInputedDetail on new { ordered.Orderid, ordered.ItemId } equals new { Orderid = inputed.OrderId, inputed.ItemId } into OrderedInputed
                          from noninput in OrderedInputed.DefaultIfEmpty()
                          select new { ordered, noninput })
                     join g in GlobalApp.AllGoods on d.ordered.ItemId equals g.Code
                     where d.noninput == null
                     select new PurchaseInputReport
                     {
                         OrderDate = d.ordered.PurchaseOrder.Orderdate,
                         OrderId = d.ordered.Orderid,
                         Supplierid = d.ordered.PurchaseOrder.Supplier.Id,
                         ItemId = d.ordered.ItemId,
                         InputQuantity = 0,
                         TotalInputPrice = 0,
                         OrderQuantity = d.ordered.Quantity,
                         OrderPrice = d.ordered.UnitPrice,
                         TotalPriceVat = d.ordered.AmountIncludedVat,
                         ItemName = g.Name,
                         SupplierName = d.ordered.PurchaseOrder.Supplier.Name,
                         ExistingQty = d.ordered.Quantity,
                         TotalExistingPrice = d.ordered.AmountIncludedVat,
                         RealVat = d.ordered.Vat
                     });

            return neverInput.ToList();
        }

        /// <summary>
        /// sum inputed total detail by good
        /// </summary>
        /// <param name="inputedDetail"></param>
        /// <returns></returns>
        private IList<PurchaseInputedTotalRecord> GetSumInputedTotalByGood(IList<PurchaseInputReport> inputedDetail)
        {
            // get sum from detail
            var sumOrdered =
                    (from o in
                         (
                             from i in inputedDetail
                             group i by new { i.ItemId, i.OrderId, i.OrderQuantity, i.TotalPriceVat } into OrderedDetail
                             select new
                             {
                                 ItemId = OrderedDetail.Select(d => d.ItemId).FirstOrDefault(),
                                 OrderId = OrderedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                 OrderQuantity = OrderedDetail.Select(d => d.OrderQuantity.Value).FirstOrDefault(),
                                 TotalPrice = OrderedDetail.Select(d => d.TotalPriceVat.Value).FirstOrDefault()
                             }
                        )
                     group o by o.ItemId into Ordered
                     select new
                     {
                         ItemId = Ordered.Select(o => o.ItemId).FirstOrDefault(),
                         TotalOrderedQuantity = Ordered.Sum(o => o.OrderQuantity),
                         TotalOrderedPrice = Ordered.Sum(o => o.TotalPrice)
                     });

            var sumInputed =
                    (from i in inputedDetail
                     group i by i.ItemId into Inputed
                     select new
                     {
                         ItemId = Inputed.Select(i => i.ItemId).FirstOrDefault(),
                         TotalInputedQuantity = Inputed.Sum(i => i.InputQuantity.Value),
                         TotalInputedPrice = Inputed.Sum(i => i.TotalInputPrice.Value)
                     });

            var sumExisting =
                    (from f in
                         (
                             (from i in inputedDetail
                              group i by new { i.ItemId, i.OrderId } into InputedDetail
                              select new
                              {
                                  ItemId = InputedDetail.Select(s => s.ItemId).FirstOrDefault(),
                                  OrderId = InputedDetail.Select(s => s.OrderId).FirstOrDefault(),
                                  ExistingQuantity = InputedDetail.Min(s => s.ExistingQty),
                                  ExistingPrice = InputedDetail.Min(s => s.TotalExistingPrice)
                              })
                         )
                     group f by f.ItemId into Existing
                     select new
                     {
                         ItemId = Existing.Select(s => s.ItemId).FirstOrDefault(),
                         TotalExistingQuantity = Existing.Sum(s => s.ExistingQuantity.Value),
                         TotalExistingPrice = Existing.Sum(s => s.ExistingPrice.Value)
                     });

            var sumInputedTotal =
                    (from o in sumOrdered
                     join i in sumInputed on o.ItemId equals i.ItemId
                     join e in sumExisting on i.ItemId equals e.ItemId
                     join g in GlobalApp.AllGoods on e.ItemId equals g.Code
                     orderby o.ItemId
                     select new PurchaseInputedTotalRecord
                     {
                         ObjectId = o.ItemId,
                         ObjectName = g.Name,
                         SumOrderQuantity = o.TotalOrderedQuantity,
                         SumOrderPrice = o.TotalOrderedPrice,
                         SumInputedQuantity = i.TotalInputedQuantity,
                         SumInputedPrice = i.TotalInputedPrice,
                         SumExistingQuantity = e.TotalExistingQuantity,
                         SumExistingPrice = e.TotalExistingPrice
                     }).ToList<PurchaseInputedTotalRecord>();

            return sumInputedTotal;
        }

        /// <summary>
        /// sum inputed total detail by supplier
        /// </summary>
        /// <param name="inputedDetail"></param>
        /// <returns></returns>
        private IList<PurchaseInputedTotalRecord> GetSumInputedTotalBySupplier(IList<PurchaseInputReport> inputedDetail)
        {
            // get sum from detail
            var sumOrdered =
                    (from o in
                         (
                             from i in inputedDetail
                             group i by new { i.Supplierid, i.OrderId, i.OrderQuantity, i.TotalPriceVat } into OrderedDetail
                             select new
                             {
                                 SupplierId = OrderedDetail.Select(d => d.Supplierid).FirstOrDefault(),
                                 OrderId = OrderedDetail.Select(d => d.OrderId).FirstOrDefault(),
                                 OrderQuantity = OrderedDetail.Select(d => d.OrderQuantity.Value).FirstOrDefault(),
                                 TotalPrice = OrderedDetail.Select(d => d.TotalPriceVat.Value).FirstOrDefault()
                             }
                        )
                     group o by o.SupplierId into Ordered
                     select new
                     {
                         SupplierId = Ordered.Select(o => o.SupplierId).FirstOrDefault(),
                         TotalOrderedQuantity = Ordered.Sum(o => o.OrderQuantity),
                         TotalOrderedPrice = Ordered.Sum(o => o.TotalPrice)
                     });

            var sumInputed =
                    (from i in inputedDetail
                     group i by i.Supplierid into Inputed
                     select new
                     {
                         SupplierId = Inputed.Select(i => i.Supplierid).FirstOrDefault(),
                         TotalInputedQuantity = Inputed.Sum(i => i.InputQuantity.Value),
                         TotalInputedPrice = Inputed.Sum(i => i.TotalInputPrice.Value)
                     });

            // group by i.Supplierid, i.OrderId, i.ItemId
            var sumExisting =
                    (from f in
                         (
                             (from i in inputedDetail
                              group i by new { i.Supplierid, i.OrderId, i.ItemId } into InputedDetail
                              select new
                              {
                                  SupplierId = InputedDetail.Select(s => s.Supplierid).FirstOrDefault(),
                                  OrderId = InputedDetail.Select(s => s.OrderId).FirstOrDefault(),
                                  ItemId = InputedDetail.Select(s => s.ItemId).FirstOrDefault(),
                                  ExistingQuantity = InputedDetail.Min(s => s.ExistingQty),
                                  ExistingPrice = InputedDetail.Min(s => s.TotalExistingPrice)
                              })
                         )
                     group f by f.SupplierId into Existing
                     select new
                     {
                         SupplierId = Existing.Select(s => s.SupplierId).FirstOrDefault(),
                         TotalExistingQuantity = Existing.Sum(s => s.ExistingQuantity.Value),
                         TotalExistingPrice = Existing.Sum(s => s.ExistingPrice.Value)
                     });

            var sumInputedTotal =
                    (from o in sumOrdered
                     join i in sumInputed on o.SupplierId equals i.SupplierId
                     join e in sumExisting on i.SupplierId equals e.SupplierId
                     join s in GlobalApp.AllSuppliers on e.SupplierId equals s.Id
                     orderby o.SupplierId
                     select new PurchaseInputedTotalRecord
                     {
                         ObjectId = o.SupplierId,
                         ObjectName = s.Name,
                         SumOrderQuantity = o.TotalOrderedQuantity,
                         SumOrderPrice = o.TotalOrderedPrice,
                         SumInputedQuantity = i.TotalInputedQuantity,
                         SumInputedPrice = i.TotalInputedPrice,
                         SumExistingQuantity = e.TotalExistingQuantity,
                         SumExistingPrice = e.TotalExistingPrice
                     }).ToList<PurchaseInputedTotalRecord>();

            return sumInputedTotal;
        }
    }
}